<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: HP
 * Date: 2017/6/22
 * Time: 16:54
 */
class Brand extends Admin_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->library('Pagination');
        $this->load->library('Upload');
        $this->load->model('Brand_model');
    }
    //增
    public function brand_add(){
        if ($_POST){
            $this->form_validation->set_rules('brand_name', '品牌名称', 'required');
            if ($this->form_validation->run() == FALSE){
                $data['message'] = validation_errors();
                $data['url'] = site_url('Admin/Brand/brand_add');
                $data['wait'] = 3;
                $this->load->view('message.html',$data);
            }else{
                if($this->upload->do_upload('logo')){
                    # 上传成功，获取文件名
                    $fileinfo = $this->upload->data();
                    $data['logo'] = $fileinfo['file_name'];
                    #获取表单提交数据
                    $data['brand_name'] = $this->input->post('brand_name',true);
                    $data['url'] = $this->input->post('url');
                    $data['brand_desc'] = $this->input->post('brand_desc',true);
                    $data['sort_order'] = $this->input->post('sort_order');
                    $data['is_show'] = $this->input->post('is_show');
                    if ($this->Brand_model->add_oneBrand($data)){
                        unset($data);
                        $data['message'] = "添加品牌成功";
                        $data['url'] = site_url('Admin/Brand/brand_list');
                        $data['wait'] = 3;
                        $this->load->view('message.html',$data);
                    }else{
                        $img = './uploads/'.$data['logo'];
                        @unlink ($img);
                        unset($data);
                        $data['message'] = "添加品牌失败";
                        $data['url'] = site_url('Admin/Brand/brand_add');
                        $data['wait'] = 3;
                        $this->load->view('message.html',$data);
                    }
                }else{
                    # 上传失败
                    $data['message'] = $this->upload->display_errors();
                    $data['wait'] = 3;
                    $data['url'] = site_url('Admin/Brand/brand_add');
                    $this->load->view('message.html',$data);
                }
            }
        }else{
            $this->load->view('Goods/brand_add.html');
        }
    }
    //删
    public function brand_delete(){

    }
    //改
    public function brand_edit(){
        if ($_POST){

        }else{
            $this->load->view('Goods/brand_edit.html');
        }
    }
    //查
    public function brand_list(){
        $data['brands'] = $this->Brand_model->list_brand();
        $this->load->view('Goods/brand_list.html',$data);
    }
}